/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dal;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Kien Chinh
 */
public class ConnectDB {

    private static Connection con;
    private static Properties prop = new Properties();

    public static Connection getConnect(){
        try {
            prop.load(new FileInputStream("config.properties"));
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection("jdbc:sqlserver://" + prop.getProperty("ip", "localhost") + ":" + prop.getProperty("port", "1433") + ";databaseName=LeaveManagementSystem", "" + prop.getProperty("userName", "sa") + "", "" + prop.getProperty("password", "123456") + "");
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        }
        return con;
    }
    
    public static boolean testConnect() {
        try {
            prop.load(new FileInputStream("config.properties"));
            Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
            con = DriverManager.getConnection("jdbc:sqlserver://" + prop.getProperty("ip", "localhost") + ":" + prop.getProperty("port", "1433") + ";databaseName=LeaveManagementSystem", "" + prop.getProperty("userName", "sa") + "", "" + prop.getProperty("password", "123456") + "");
            return true;
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            return false;
        } catch (SQLException ex) {
            return false;
        }
        return false;
    }
    
    public static boolean closeConnect() {
        if (con != null) {
            try {
                con.close();
                return true;
            } catch (SQLException ex) {
                Logger.getLogger(ConnectDB.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        return false;
    }
}
